package com.modifiertech.lineage.network;

import com.modifiertech.lineage.Lineage;
import com.modifiertech.lineage.api.LineageModAccess;
import com.modifiertech.lineage.api.PlayerData;
import com.modifiertech.lineage.event.PlayerDataHandler;
import io.netty.buffer.ByteBuf;
import net.minecraft.client.Minecraft;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.fml.common.network.ByteBufUtils;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

public class MessageDataSync implements IMessage, IMessageHandler<MessageDataSync,IMessage> {
    public NBTTagCompound nbtTagCompound;
    public MessageDataSync(){
    }
    public MessageDataSync(PlayerData playerData){
        nbtTagCompound=new NBTTagCompound();
        playerData.saveToNBT(nbtTagCompound);
    }

    @Override
    public void fromBytes(ByteBuf byteBuf) {
        nbtTagCompound = ByteBufUtils.readTag(byteBuf);
    }

    @Override
    public void toBytes(ByteBuf byteBuf) {
        ByteBufUtils.writeTag(byteBuf, nbtTagCompound);
    }

    @Override
    public IMessage onMessage(MessageDataSync messageDataSync, MessageContext messageContext) {
        Minecraft.getMinecraft().addScheduledTask(() -> handleMessage(messageDataSync));
        return null;
    }

    public void handleMessage(MessageDataSync message) {
        Lineage.proxy.handleOnMessageDataSync(message);
    }
}
